<div class="head">Редактирование очереди</div>
<div class="cont_body" style="padding: 10px">
    <? if ($this->message) : ?>
        <div class="msg"><? echo $this->message ?></div>
    <? endif; ?>
    <form class="form-horizontal" id="form" method="post">
    <fieldset>
        <div class="control-group">
        <label class="control-label" for="turn_name">Название очереди</label>
        <div class="controls">
            <input type="text" class="input-xlarge" id="turn_name" name="name" value="<? echo $this->turn_name ?>">
        </div>
        </div>
        
        <div class="control-group">
            <label class="control-label" for="group">Группа</label>
            <div class="controls">
                <select id="group" name="group" style="width: 200px;">
                <?php foreach($this->groups as $group): ?>
                    <option id="<?php echo $group->vk_id; ?>" <?php echo ($group->id == $this->group) ? 'selected' : '';?>><?php echo $group->name; ?></option>
                <?php endforeach; ?>
                </select>
            </div>
        </div>
        
        <div class="control-group">
            <label class="control-label" for="time">Время</label>
            <div class="controls">
                <textarea id="time" name="time" style="height: 120px; width: 120px;" value=""><? if ($this->times->count()): foreach($this->times as $time): ?><? $t = substr($time->time, 0, -3); echo $this->escape($t), "\n" ?><? endforeach; endif; ?></textarea>
            </div>
        </div>
        
        <div class="form-actions">
            <input type="hidden" id="tid" name="tid" value="<? echo $this->tid ?>" />
            <input type="submit" id="send" class="btn btn-primary" value="Сохранить" />
        </div>
    </fieldset>
    </form>
</div>
<div class="clear"></div>
<?php if($this->posts): ?>
<ul>
	<?php foreach($this->posts as $id => $post): ?>
	<li style="border-top: 1px solid; border-bottom: 1px solid; border-color: #07D; padding: 10px;">
		<h3><input type="text" id="title" value="<? echo $post['text'] ?>"/></h3>
		<center><img id="post_img" style="max-height: 500px; max-width: 500px;" src="<?php echo $post['image'] ?>" /></center>
		<div>
            <span>Дата постинга: <input class="input-small" type="text" id="post_date" value="<? echo $post['post_date'] ?>"/>, </span>
            <span>Время постинга: <input class="input-small" type="text" id="post_time" value="<? echo substr($post['post_time'], 0, 5) ?>"/></span>
		</div>
		<div style="float: right">
			<a href="#" onclick="turn_post_edit(this, '<? echo $post['id'] ?>'); return false;">Сохранить</a>
			<a href="#" onclick="turn_post_delete(this, '<? echo $post['id'] ?>'); return false;">Удалить</a>
			<div class="progress"></div>
		</div>
		<div class="clear"></div>
	</li>
	<?php endforeach; ?>
</ul>
<?php endif; ?>
<script>
var tid = '<?php echo $this->tid ?>';
function turn_post_edit(el, pid) {
	$(el).parent().find('a').hide().parent().find('div').show();
	var post_date = $(el).parent().parent().find('#post_date').val(), post_time = $(el).parent().parent().find('#post_time').val();
	if (post_date == '') {
		$(el).parent().parent().find('#post_date').focus();
		return;
	}
	if (post_time == '') {
		$(el).parent().parent().find('#post_time').focus();
		return;
	}
	$.ajax({
		url: '/turn/edit.post',
		type: 'post',
		data: {
			tid: tid,
			post_id: pid,
			text: $(el).parent().parent().find('#title').val(),
			date: post_date,
			time: post_time
		},
		dataType: 'json',
		success: function(response){
			if (response.body == 'ok') {
				$(el).parent().find('div').hide().parent().find('a').show();
				return;
			}
		},
		error: function(response){
			alert('Какая то ошибка');
			$(el).parent().find('div').hide().parent().find('a').show();
		}
	});
}
function turn_post_delete(el, pid) {
	$(el).parent().find('a').hide().parent().find('div').show();
	$.ajax({
		url: '/turn/delete.post',
		type: 'post',
		data: {
			tid: tid,
			post_id: pid
		},
		dataType: 'json',
		success: function(response){
			if (response.body == 'ok') {
				$(el).parent().parent().remove();
				return;
			}
		},
		error: function(response){
			alert('Какая то ошибка');
			$(el).parent().find('div').hide().parent().find('a').show();
		}
	});
}
</script>